<template>
  <!-- 七日店铺交易金额 -->
  <div id="MoneyCharts" class="MoneyCharts" />
</template>

<script>
import { getStoreUSDMillions } from "@/api/user";
import { rsaDecrypt } from "@/utils/commonutils3";
import { getToken } from "@/utils/auth";
import * as echarts from "echarts/core";
import {
  TooltipComponent,
  GridComponent,
  LegendComponent,
} from "echarts/components";
import { BarChart, LineChart } from "echarts/charts";
import { UniversalTransition } from "echarts/features";
import { CanvasRenderer } from "echarts/renderers";

echarts.use([
  TooltipComponent,
  GridComponent,
  LegendComponent,
  BarChart,
  LineChart,
  CanvasRenderer,
  UniversalTransition,
]);
export default {
  name: "MoneyCharts",
  data() {
    return {
      list: [],
      leftMax: null,
      rightMax: null,
      leftInterval: null,
      orderCountList: [],
    };
  },
  mounted() {
    if (getToken()) {
      this.$nextTick(() => {
        this.getStoreUSDMillions();
      });
    }
  },
  methods: {
    async getStoreUSDMillions() {
      const res = await getStoreUSDMillions();
      if (res.code === 200) {
        console.log(res.data, "订单交易金额");
        const list = res.data.screenTradeList.reverse();
        if (list.length == 0) return
        let ydata = [];
        let ydata1 = [];
        let xdata = [];
        list.forEach((item) => {
          ydata.push((item.salesVolume / 1000).toFixed(2));
          xdata.push(item.day.substring(5));
          ydata1.push(item.orderSum);
        });
        this.leftMax = (Math.max(...ydata) + 2).toFixed(2);
        this.rightMax = Math.max(...ydata1);
        this.leftInterval = parseInt((this.leftMax + 1) / 5);
        this.rightInterval = parseInt((this.rightMax + 10) / 5);
        console.log(this.leftMax, this.rightMax, this.leftInterval, '最后')
        console.log('xdata, ydata, ydata1', xdata, ydata, ydata1);
        this.$nextTick(() => {

          this.initchart(xdata, ydata, ydata1);
        });
      }
    },
    initchart(xdata, ydata, ydata1) {
      console.log('this.leftMax', this.leftMax, this.rightMax);
      console.log('110', xdata, ydata, ydata1);
      const option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
          formatter: "{a0} : {c0} 千元<br />{a3} : {c3} 个",
        },
        legend: {
          right: "8%",
          data: [
            {
              name: "单日营业额",
              // 强制设置图形为圆。
              icon: "roundRect",
              textStyle: {
                color: "rgba(255, 255, 255, 0.90)",
              },
              itemStyle: {
                color: "rgba(58, 244, 233, 1)",
              },
              // 设置文本为红色
            },
            {
              name: "单日订单",
              // 强制设置图形为圆。
              icon: "roundRect",
              textStyle: {
                color: "rgba(255, 255, 255, 0.90)",
              },
              itemStyle: {
                color: "rgba(221, 58, 241, 1)",
              },
              // 设置文本为红色
            },
          ],
        },
        grid: {
          left: "7%",
          right: "7%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: xdata,
            axisTick: {
              show: false,
              // inside:true
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "(千元)",
            smooth: true,
            alignTicks: true,
            min: 0.00,
            max: this.leftMax,
            minInterval: 0.01,
            // axisLabel: {
            //   formatter: "{value}",
            // },
            axisLabel: {
              formatter: function (value, index) {
                console.log('value', index, value);
                if (value) {

                  return value?.toFixed(2);
                }
                //保留一位小数     
              }
            },
            splitLine: {
              // 网格线
              show: false, // 关闭网格线
            },
          },
          {
            type: "value",
            name: "(个)",
            alignTicks: true,
            min: 0,
            max: this.rightMax + 10,
            interval: this.rightInterval,
            axisLabel: {
              formatter: "{value}",
            },
            splitLine: {
              // 网格线
              show: true, // 关闭网格线
              // 或者
              lineStyle: {
                type: "dashed", // 设置网格线类型 dotted：虚线   solid:实线
                color: "rgba(0, 0, 0, 0.1)",
              },
            },
          },
        ],
        series: [
          {
            name: "单日营业额",
            type: "bar",
            emphasis: {
              focus: "series",
            },
            barWidth: 45,
            itemStyle: {
              // lenged文本
              opacity: 1,
              color: (e) => {
                // console.log('index', index);
                // const index = e.dataIndex;
                return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: "rgba(1,245,255,1)", // 0% 处的颜色
                  },
                  {
                    offset: 0.3,
                    color: "rgba(1,245,255,0.5)", // 100% 处的颜色
                  },
                  {
                    offset: 0.7,
                    color: "rgba(42, 120, 254, 0.5)", // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "rgba(42, 120, 254, 0.8)", // 100% 处的颜色
                  },
                ]);
              },
            },
            z: 12,
            color: "rgba(58, 244, 233, 1)",
            data: ydata,
          },
          {
            name: "",
            type: "pictorialBar",
            symbolSize: [45, 10],
            symbolOffset: [0, -5],
            z: 18,
            itemStyle: {
              opacity: 1,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "#91FBFF", // 0% 处的颜色
                },
                {
                  offset: 1,
                  color: "#01F5FF", // 100% 处的颜色
                },
              ]),
            },
            symbolPosition: "end",
            data: ydata,
          },
          {
            name: "",
            type: "pictorialBar",
            symbolSize: [45, 10],
            symbolOffset: [0, -5],
            z: 18,
            itemStyle: {
              opacity: 1,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "rgba(42, 120, 254, 0.8)", // 0% 处的颜色
                },
                {
                  offset: 1,
                  color: "rgba(42, 120, 254, 0.8)", // 100% 处的颜色
                },
              ]),
            },
            symbolPosition: "end",
            data: [0, 0, 0, 0, 0, 0, 0],
          },
          {
            name: "单日订单",
            type: "line",
            z: 99,
            yAxisIndex: 1,
            showSymbol: false,
            areaStyle: {
              // 区域填充渐变颜色
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "rgba(221, 58, 241, 1)", // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "rgba(221, 58, 241, 0)", // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              },
            },
            color: "rgba(221, 58, 241, 1)",
            lineStyle: {
              width: 3, // 外边线宽度
              color: "rgba(221, 58, 241, 1)", // 外边线颜色
            },
            tooltip: {
              valueFormatter: function (value) {
                return value + "";
              },
            },
            data: ydata1,
          },
        ],
      };
      // var chartDom = null

      let myChart = echarts.getInstanceByDom(document.getElementById("MoneyCharts"))

      if (myChart) {
        myChart.setOption(option);
        return
      }
      var chartDom = document.getElementById("MoneyCharts");
      myChart = echarts.init(chartDom);
      option && myChart.setOption(option);
    },
  },
};
</script>

<style scoped lang="scss">
.MoneyCharts {
  width: 2.9583rem;
  height: 1.25rem;
  margin: 0 auto;
}
</style>
